# -*- coding: utf-8 -*-
# @author: HRUN

import json
import os

def convert_null_strings(obj):
    """递归转换字符串'null'为None"""
    if isinstance(obj, dict):
        return {k: convert_null_strings(v) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [convert_null_strings(item) for item in obj]
    elif obj == "null":
        return None
    else:
        return obj

def get_data_file_path():
    """获取data.json文件的路径"""
    # 获取当前文件所在目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(current_dir, 'data.json')

def save_data(data):
    # 处理数据中的字符串'null'，转换为None
    processed_data = convert_null_strings(data)
    
    # 获取文件路径
    file_path = get_data_file_path()
    
    # 每次运行时都会覆盖之前的内容
    with open(file_path, 'w', encoding='utf-8') as f:
        json.dump(processed_data, f, ensure_ascii=False, indent=4)

def load_data():
    try:
        file_path = get_data_file_path()
        with open(file_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        return None